package com.clgc.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.clgc.eduservice.entity.*;
import com.clgc.eduservice.service.*;
import com.clgc.utils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.POST;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author cml
 * @create 2021/5/14 0:43
 */
@Api("课程班级")
@RestController
@RequestMapping("/eduservice/edu-kcbj")
public class EduKcbjController {
    @Autowired
    EduKcbjService eduKcbjService;

    @Autowired
    CourseClassService courseClassService;

    @Autowired
    TeacherClassService teacherClassService;

    @Autowired
    KcbjStudentService kcbjStudentService;

    /**
     * 创建课程班级
     * @param eduKcbj
     * @param courseId
     * @param teacherId
     * @return
     */
    @PostMapping("/kcbj/{teacherId}/{courseId}")
    @Transactional(rollbackFor = Exception.class)
    public R createKcbj(@RequestBody EduKcbj eduKcbj, @PathVariable("courseId") String courseId,
                        @PathVariable("teacherId") Integer teacherId){
        if(eduKcbj.getName() != null && eduKcbj.getName() != "" && courseId != null && teacherId != null ){
            boolean save = eduKcbjService.save(eduKcbj);

            CourseClass courseClass = new CourseClass(courseId,eduKcbj.getId());
            boolean save1 = courseClassService.save(courseClass);
//            int a=10/0;
            TeacherClass teacherClass = new TeacherClass(eduKcbj.getId(), teacherId);
            boolean save2 = teacherClassService.save(teacherClass);

            return save&&save1&&save2 ? R.ok():R.error().message("班级创建失败");
        }
        return R.error().message("课程名或courseId为空");
    }

    /**
     * 根据课程id获取 该课程下所有课程班级
     * @param courseId
     * @return
     */
    @GetMapping("/kcbj/{courseId}")
    public R getKcbj(@PathVariable("courseId") String courseId){
       List<EduKcbj> ListKcbj = courseClassService.getCourseAllKcbj(courseId);
        return R.ok().data("data",ListKcbj);
    }

    /**
     * 根据课程班级id 获取该课程班级下的所有学生
     * @param id
     * @return
     */
    @GetMapping("/kcbj/student/{id}")
    public R getStudents(@PathVariable("id") String id){
        List<EduStudent> studentList = kcbjStudentService.getStudentsByKcbjId(id);

        EduKcbj eduKcbj = eduKcbjService.getById(id);
        ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
        map.put("id",eduKcbj.getId());
        map.put("KcbjName",eduKcbj.getName());
        map.put("studentNum",eduKcbj.getStuNum());
        map.put("student",studentList);
        return R.ok().data("data",map);
    }

    /**
     * 根据课程班级id删除该课程班级
     * @param id
     * @return
     */
    @DeleteMapping("/kcbj/{id}")
    public R deleteKcbj(@PathVariable("id") String id){
        boolean b = eduKcbjService.removeById(id);
        return b?R.ok():R.error().message("删除课程班级失败");
    }

    @PostMapping("/kcbj/update")
    public R updateKcbj(@RequestBody EduKcbj eduKcbj){
        boolean update = eduKcbjService.update(eduKcbj,new QueryWrapper<EduKcbj>().eq("id",eduKcbj.getId()));
        return update?R.ok():R.error().message("修改课程班级名 失败");
    }
}
